package io.renren.modules.rps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.rps.dao.SiteDailyDao;
import io.renren.modules.rps.dto.SiteDailyDTO;
import io.renren.modules.rps.entity.SiteDailyEntity;
import io.renren.modules.rps.service.SiteDailyService;
import io.renren.modules.security.password.PasswordUtils;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.security.user.UserDetail;
import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.enums.SuperAdminEnum;
import io.renren.modules.sys.service.SysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * site日报管理
 *
 * @author HuYJ huyongjie@qone-inst.com
 * @since 1.0.0 2022-09-17
 */
@Service
public class SiteDailyServiceImpl extends BaseServiceImpl<SiteDailyDao, SiteDailyEntity> implements SiteDailyService {
    @Autowired
    private SysDeptService sysDeptService;

    @Override
    public PageData<SiteDailyDTO> page(Map<String, Object> params) {
        //分页
        IPage<SiteDailyEntity> page = getPage(params, Constant.CREATE_DATE, false);

        //普通管理员，只能查询所属部门及子部门的数据
        UserDetail user = SecurityUser.getUser();
        if(user.getSuperAdmin() == SuperAdminEnum.NO.value()) {
            params.put("deptIdList", sysDeptService.getSubDeptIdList(user.getDeptId()));
        }
        //查询
        List<SiteDailyEntity> list = baseDao.getList(params);

        return getPageData(list, page.getTotal(), SiteDailyDTO.class);
    }

    @Override
    public List<SiteDailyDTO> list(Map<String, Object> params) {
        //普通管理员，只能查询所属部门及子部门的数据
        UserDetail user = SecurityUser.getUser();
        if(user.getSuperAdmin() == SuperAdminEnum.NO.value()) {
            params.put("deptIdList", sysDeptService.getSubDeptIdList(user.getDeptId()));
        }

        List<SiteDailyEntity> entityList = baseDao.getList(params);

        return ConvertUtils.sourceToTarget(entityList, SiteDailyDTO.class);
    }

    @Override
    public SiteDailyDTO get(Long id) {
        SiteDailyEntity entity = baseDao.getById(id);

        return ConvertUtils.sourceToTarget(entity, SiteDailyDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SiteDailyDTO dto) {
        SiteDailyEntity entity = ConvertUtils.sourceToTarget(dto, SiteDailyEntity.class);

        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SiteDailyDTO dto) {
        SiteDailyEntity entity = ConvertUtils.sourceToTarget(dto, SiteDailyEntity.class);

        //更新日报数据
        updateById(entity);

    }

    @Override
    public void delete(Long[] ids) {
        //删除日报数据
        baseDao.deleteBatchIds(Arrays.asList(ids));
    }
}